package com.zjuee.web.basic.file.util;


import org.apache.commons.lang3.StringUtils;

/**
 * @author wu liang
 * @since 1.0
 */
public class StringsUtil {

    /**
     * 替换字符串中的特殊符号
     * @param str
     * @return
     */
    public static String replaceAllStr(String str) {
        if (StringUtils.isNotBlank(str)) {
            str = str.replaceAll("\\\\", "").replaceAll(":", "")
                    .replaceAll("&", "").replaceAll("=", "")
                    .replaceAll("\\+", "").replaceAll("\\|", "")
                    .replaceAll("%", "").replaceAll("/", "")
                    .replaceAll("\\?", "");
            return str;
        } else {
            return str;
        }
    }

    /**
     * 通过url截取文件名
     *
     * @param url
     * @return
     */
    public static String subUrl(String url) {
        if (StringUtils.isEmpty(url)) {
            return null;
        }

        int urlIndex = url.lastIndexOf("/");
        String fileName = url.substring(urlIndex + 1, url.length());
        int fileIndex = fileName.lastIndexOf("_");

        if (fileIndex == -1) {
            return fileName;
        }
        String[] ext = fileName.split("\\.");

        if (ext.length < 2) {
            return fileName;
        }
        return fileName.substring(0, fileIndex) + "." + ext[1];
    }
}
